<template>
  <div class="user-container">
    <div class="info-box">
      <van-image
        width="64"
        height="64"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <h3>
        {{ userInfo.name }}
        <br />
        <van-tag color="white" text-color="#3296fa">{{
          userInfo.birthday
        }}</van-tag>
      </h3>
    </div>
    <!-- 布局 -->
    <van-row class="user-link">
      <van-col span="8">
        <van-icon name="newspaper-o" color="#8eb1ff" />
        <br />
        我的作品
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#ff050e" />
        <br />
        我的收藏
      </van-col>
      <van-col span="8">
        <van-icon name="tosend" color="#ffaa14" />
        <br />
        阅读历史
      </van-col>
    </van-row>
    <!-- 单元格 -->
    <van-cell-group>
      <van-cell title="编辑资料" to="/edit" is-link icon="edit" />
      <van-cell title="小智同学" to="/student" is-link icon="chat-o" />
      <van-cell title="系统设置" is-link icon="setting-o" />
      <van-cell @click="showDialog" title="退出登录" is-link icon="warning-o" />
    </van-cell-group>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user'
import { removeToken } from '@/utils/token'
export default {
  name: 'user',
  created () {
    getUserInfo().then(res => {
      this.userInfo = res.data.data
      this.$store.commit('setUserInfo', res.data.data)
    })
  },
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    showDialog () {
      this.$dialog
        .confirm({
          message: '确定要退出登录吗？'
        })
        .then(() => {
          // 清除token
          removeToken()
          // 清除用户信息
          this.userInfo = {}
          // 跳转登录页
          this.$router.push({ path: '/login' })
        })
        .catch(() => {
          console.log('取消')
        })
    }
  }
}
</script>

<style lang="less">
.user-container {
  height: 100px;
  background-color: #3296fa;
  .info-box {
    display: flex;
    align-items: center;
    font-size: 16px;
    color: white;
    .van-image {
      margin: 20px 15px;
    }
    h3 {
      font-weight: normal;
    }
  }
}
.user-link {
  font-size: 14px;
  text-align: center;
  padding: 20px 0;
  .van-icon {
    font-size: 30px;
  }
}
</style>
